#! /bin/bash

# 预创建目录
create_dir(){
    sudo mkdir -p ./{ceph1,ceph2,ceph3}/{etc/ceph,var/lib/ceph,mnt/cephfs}
    sudo chmod -R 777 ./{ceph1,ceph2,ceph3}
}

copy_ceph(){
    cd $local_dir/ceph1
    cat >> etc/ceph/ceph.conf<<EOF
osd max object name len = 256
osd max object namespace len = 64
EOF

    sudo cp $local_dir/ceph1/etc/ceph/*   $local_dir/ceph2/etc/ceph/
    sudo cp $local_dir/ceph1/etc/ceph/*   $local_dir/ceph3/etc/ceph/
    sudo cp -r $local_dir/ceph1/var/lib/ceph/bootstrap-* $local_dir/ceph2/var/lib/ceph/
    sudo cp -r $local_dir/ceph1/var/lib/ceph/bootstrap-* $local_dir/ceph3/var/lib/ceph/

    # 重启
    docker-compose restart

    # 启动dashboard
    docker-compose exec ceph-mon ceph mgr dump
    docker-compose exec ceph-mgr ceph mgr module enable dashboard

}

# 启动ceph
start_ceph_master(){
    cd $local_dir/ceph1
    ceph_osd_state=$(docker-compose  ps | grep ceph-osd | awk '{print $3}')
    if [ '$ceph_osd_state' = 'osd_directory' ]
    then
        docker-compose up -d
        cd $local_dir
    else
        docker-compose up -d
    fi
        
}

start_ceph_slave(){
    cd $local_dir/ceph2
    docker-compose up -d

    cd $local_dir/ceph3
    docker-compose up -d

    cd $local_dir
}


stop_ceph(){
    for i in $(seq 1 3)
    do
        cd $local_dir/ceph$i
        docker-compose down
    done
    cd $local_dir
}

# 启动函数
main(){
    local_dir=$(pwd)
    docker-compose  -f ceph1/docker-compose.yml ps | grep ceph > /dev/null
    if [ $? != 0 ]
    then
        create_dir
        start_ceph_master
        # clear
        # 添加参数和copy配置文件
        copy_ceph
        start_ceph_slave
        # clear
    else
        echo "Confirm to stop the current service!  yes | no"
        read -p "> "  selet
        case $selet in
        y | yes)
            stop_ceph
            ;;
        n | no)
            exit 0
            ;;
        *)
            echo "请输入 yes | no "
            ;;
        esac
    fi
}

cd $(dirname $0)
main